home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / reve / rev_eval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  7.5 KB  |  238 lines

  1.  
  2. /*  @(#)rev_eval.c 1.9 91/11/07
  3.  *
  4.  *  Copyright (C) 1990, 1991 - Yves Gallot - all rights reserved.
  5.  *
  6.  *  Permission is granted to copy this source, for redistribution
  7.  *  in source form only, provided the news headers in "substantially
  8.  *  unaltered format" are retained, the introductory messages are not
  9.  *  removed, and no monies are exchanged.
  10.  *
  11.  *  Permission is also granted to copy this source, without the
  12.  *  news headers, for the purposes of making an executable copy by
  13.  *  means of compilation, provided that such copy will not be used
  14.  *  for the purposes of competition in any othello tournaments, without
  15.  *  prior permission from the authors.
  16.  *
  17.  *  No responsibility is taken for any errors on inaccuracies inherent
  18.  *  either to the comments or the code of this program, but if reported
  19.  *  (see README file), then an attempt will be made to fix them.
  20.  */
  21.  
  22. #include "reve.h"
  23.  
  24. extern int damier[NIVEAUMAX][64] ;
  25. extern int tacouleur, macouleur ;
  26. extern int mnb, profmax ;
  27. extern int vp0, vo0 ;
  28. extern long c1, c2, c3 ;
  29. extern long edges[6561] ;
  30.  
  31. /*  Evaluation function : gives a note to a board = damier[ niv ].
  32.  *  It computes 4 components :
  33.  *     - Edge Stability, using Edge Stability Table pre-computed before
  34.  *     - Current Mobility, in fact computed during alpha-beta pruning
  35.  *     - X squares Values
  36.  *     - Potential Mobility, looking at squares contacts
  37.  *  And then Eval = c1 * ES + c2 * ( CM + cx * XV ) + c3 * PM
  38.  *  Eval is replaced by - Eval if Black is replaced by White. Eval is positive
  39.  *  if macouleur ( my color ) has a good position and negative if tacouleur
  40.  *  position is good.
  41.  *  If we are "near" the end of the game, then Eval is equal to final disc
  42.  *  differential.
  43.  */
  44.  
  45. int node_count ;
  46.  
  47. long
  48. evalue(niv)
  49. int niv ;
  50. {
  51.   register int *d, *p ;
  52.   register int i, x, y, tc, mc ;
  53.   register long note ;
  54.   register int vp, vo ;
  55.   register long tnote, cx ;
  56.  
  57.   node_count++ ;
  58.   d = damier[niv] ;
  59.   tc = tacouleur ;
  60.   mc = macouleur ;
  61.  
  62.   if (mnb > 60 - profmax)
  63.     {
  64.       note = 0 ;
  65.       for (p = &d[0]; p < &d[64]; p++)
  66.          if (*p == mc) note++ ;
  67.     else if (*p == tc) note-- ;
  68.       return(note) ;
  69.     }
  70.   else
  71.     {
  72.       x = y = 0 ;
  73.       for (p = &d[0]; p < &d[64]; p++)
  74.          if (*p == mc) x++ ;
  75.     else if (*p == tc) y++ ;
  76.       if (x == 0) return(-100000000) ;
  77.       if (y == 0) return( 100000000) ;
  78.  
  79.       for (p = &d[0]; p < &d[64]; p++)
  80.         if ((*p == TPJ) || (*p == JPJ)) *p = FREE ;
  81.  
  82.       i = 0 ;
  83.       for (p = &d[0]; p < &d[8]; p++)
  84.         {
  85.           i += i + i ;
  86.            if (*p == FREE) i++ ;
  87.       else if (*p == mc)   i += 2 ;
  88.         }
  89.       note = edges[i] ;
  90.  
  91.       i = 0 ;
  92.       for (p = &d[56]; p < &d[64]; p++)
  93.         {
  94.           i += i + i ;
  95.            if (*p == FREE) i++ ;
  96.       else if (*p == mc)   i += 2 ;
  97.         }
  98.       note += edges[i] ;
  99.  
  100.       i = 0 ;
  101.       for (p = &d[0]; p < &d[64]; p += 8)
  102.         {
  103.           i += i + i ;
  104.            if (*p == FREE) i++ ;
  105.       else if (*p == mc)   i += 2 ;
  106.         }
  107.       note += edges[i] ;
  108.  
  109.       i = 0 ;
  110.       for (p = &d[7]; p < &d[64]; p += 8)
  111.         {
  112.           i += i + i ;
  113.            if (*p == FREE) i++ ;
  114.       else if (*p == mc)   i += 2 ;
  115.         }
  116.       note += edges[i] ;
  117.  
  118.       cx = 8 * (50 - mnb - profmax) ;
  119.  
  120.       if (d[0] == FREE)
  121.         {
  122.            if (d[9] == mc) note -= cx ;
  123.       else if (d[9] == tc) note += cx ;
  124.         }
  125.  
  126.       if (d[7] == FREE)
  127.         {
  128.            if (d[14] == mc) note -= cx ;
  129.       else if (d[14] == tc) note += cx ;
  130.         }
  131.  
  132.       if (d[56] == FREE)
  133.         {
  134.            if (d[49] == mc) note -= cx ;
  135.       else if (d[49] == tc) note += cx ;
  136.         }
  137.  
  138.       if (d[63] == FREE)
  139.         {
  140.            if (d[54] == mc) note -= cx ;
  141.       else if (d[54] == tc) note += cx ;
  142.         }
  143.  
  144.       tnote = c1 * note + c2 * (long) 1000 * (vp0 - vo0) / (vp0 + vo0 + 2) ;
  145.  
  146.       vp = vo = 0 ;
  147.       for (x = 0; x < 8; x++)
  148.         for (y = 0; y < 8; y++)
  149.           {
  150.             i = (x << 3) + y ;
  151.         if (d[i] == tc)
  152.               {
  153.                      if ((x > 0) && (d[i - 8] == FREE))            vp++ ;
  154.                 else if ((x < 7) && (d[i + 8] == FREE))            vp++ ;
  155.                 else if ((y > 0) && (d[i - 1] == FREE))            vp++ ;
  156.                 else if ((y < 7) && (d[i + 1] == FREE))            vp++ ;
  157.                 else if ((x > 0) && (y > 0) && (d[i - 9] == FREE)) vp++ ;
  158.                 else if ((x < 7) && (y > 0) && (d[i + 7] == FREE)) vp++ ;
  159.                 else if ((x > 0) && (y < 7) && (d[i - 7] == FREE)) vp++ ;
  160.                 else if ((x < 7) && (y < 7) && (d[i + 9] == FREE)) vp++ ;
  161.               }
  162.         if (d[i] == mc)
  163.               {
  164.                      if ((x > 0) && (d[i - 8] == FREE))             vo++ ;
  165.                 else if ((x < 7) && (d[i + 8] == FREE))             vo++ ;
  166.                 else if ((y > 0) && (d[i - 1] == FREE))             vo++ ;
  167.                 else if ((y < 7) && (d[i + 1] == FREE))             vo++ ;
  168.                 else if ((x > 0) && (y > 0) && ( d[i - 9] == FREE)) vo++ ;
  169.                 else if ((x < 7) && (y > 0) && ( d[i + 7] == FREE)) vo++ ;
  170.                 else if ((x > 0) && (y < 7) && ( d[i - 7] == FREE)) vo++ ;
  171.                 else if ((x < 7) && (y < 7) && ( d[i + 9] == FREE)) vo++ ;
  172.               }
  173.           }
  174.  
  175.       note = ((long) 1000 * (vp - vo)) / (vp + vo + 2) ;
  176.  
  177.       vp = vo = 0 ;
  178.       for (x = 0; x < 8; x++)
  179.         for (y = 0; y < 8; y++)
  180.           {
  181.             i = (x << 3) + y ;
  182.             if (d[i] == FREE)
  183.               {
  184.              if ((x > 0) && (d[i - 8] == tc))            vp++ ;
  185.         else if ((x < 7) && (d[i + 8] == tc))            vp++ ;
  186.         else if ((y > 0) && (d[i - 1] == tc))            vp++ ;
  187.         else if ((y < 7) && (d[i + 1] == tc))            vp++ ;
  188.         else if ((x > 0) && (y > 0) && (d[i - 9] == tc)) vp++ ;
  189.         else if ((x < 7) && (y > 0) && (d[i + 7] == tc)) vp++ ;
  190.         else if ((x > 0) && (y < 7) && (d[i - 7] == tc)) vp++ ;
  191.         else if ((x < 7) && (y < 7) && (d[i + 9] == tc)) vp++ ;
  192.  
  193.              if ((x > 0) && (d[i - 8] == mc))            vo++ ;
  194.         else if ((x < 7) && (d[i + 8] == mc))            vo++ ;
  195.         else if ((y > 0) && (d[i - 1] == mc))            vo++ ;
  196.         else if ((y < 7) && (d[i + 1] == mc))            vo++ ;
  197.         else if ((x > 0) && (y > 0) && (d[i - 9] == mc)) vo++ ;
  198.         else if ((x < 7) && (y > 0) && (d[i + 7] == mc)) vo++ ;
  199.         else if ((x > 0) && (y < 7) && (d[i - 7] == mc)) vo++ ;
  200.         else if ((x < 7) && (y < 7) && (d[i + 9] == mc)) vo++ ;
  201.               }
  202.           }
  203.  
  204.       note += ((long) 1000 * (vp - vo)) / (vp + vo + 2) ;
  205.  
  206.       vp = vo = 0 ;
  207.       for (x = 0; x < 8; x++)
  208.         for (y = 0; y < 8; y++)
  209.           {
  210.             i = (x << 3) + y ;
  211.             if (d[i] == FREE)
  212.               {
  213.         if ((x > 0) && (d[i - 8] == tc))            vp++ ;
  214.         if ((x < 7) && (d[i + 8] == tc))            vp++ ;
  215.         if ((y > 0) && (d[i - 1] == tc))            vp++ ;
  216.         if ((y < 7) && (d[i + 1] == tc))            vp++ ;
  217.         if ((x > 0) && (y > 0) && (d[i - 9] == tc)) vp++ ;
  218.         if ((x < 7) && (y > 0) && (d[i + 7] == tc)) vp++ ;
  219.         if ((x > 0) && (y < 7) && (d[i - 7] == tc)) vp++ ;
  220.         if ((x < 7) && (y < 7) && (d[i + 9] == tc)) vp++ ;
  221.  
  222.         if ((x > 0) && (d[i - 8] == mc))            vo++ ;
  223.         if ((x < 7) && (d[i + 8] == mc))            vo++ ;
  224.         if ((y > 0) && (d[i - 1] == mc))            vo++ ;
  225.         if ((y < 7) && (d[i + 1] == mc))            vo++ ;
  226.         if ((x > 0) && (y > 0) && (d[i - 9] == mc)) vo++ ;
  227.         if ((x < 7) && (y > 0) && (d[i + 7] == mc)) vo++ ;
  228.         if ((x > 0) && (y < 7) && (d[i - 7] == mc)) vo++ ;
  229.         if ((x < 7) && (y < 7) && (d[i + 9] == mc)) vo++ ;
  230.               }
  231.           }
  232.  
  233.       note += ((long) 1000 * (vp - vo)) / (vp + vo + 2) ;
  234.       tnote += c3 * note ;
  235.       return(tnote / 1000) ;
  236.     }
  237. }
  238.